package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebPost;
import com.file.system.mapper.system.WebPostMapper;
import com.file.system.plugin.shiro.ShiroTools;
import com.file.system.service.system.IWebPostService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
* 岗位 实现
* @author jch
* @since 2023-10-09
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class WebPostServiceImpl implements IWebPostService {

    @Autowired
    private WebPostMapper mapper;

    @Autowired
    private SortUtil sortUtil;


    //添加岗位
    public boolean save(WebPost webPost) {
        webPost.setId(IdUtil.getSnowflakeNextId());
        //排序
        webPost = sortUtil.add(webPost, webPost.getClass(), "web_post",
            BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(webPost)) {
            return false;
        }
        return SqlHelper.retBool(mapper.save(webPost));
    }

    //添加岗位(批量)
    public boolean saveBatch(@Param("list") List<WebPost> webPostList) {
        if(webPostList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webPostList));
    }

    //添加或更新岗位
    public boolean saveOrUpdate(WebPost webPost) {
        return ObjectUtil.isNull(webPost.getId()) ? save(webPost) : updateById(webPost);
    }


    //编辑岗位
    public boolean updateById(@Param("et") WebPost webPost) {
        if (ObjectUtil.isEmpty(webPost.getId())) {
            return false;
        }
        //查询
        WebPost webPostOld = mapper.selectById(webPost.getId());
        //排序
        webPost = sortUtil.update(webPost, webPost.getClass(), webPostOld.getSort(), "web_post",
            BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(webPost)) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webPost));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebPost> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //删除岗位
    public boolean removeById(@Param("id") Long id) {
        //排序
        WebPost webPost = mapper.selectById(id);
        sortUtil.del(webPost, webPost.getClass(), "web_post",
            BaseSort.eq().val("is_del", "0")
        );
        return SqlHelper.retBool(mapper.removeById(new WebPost().setId(id)));
    }

    //通过ids删除岗位
    public boolean removeByIds(List<Long> ids) {
        UpdateWrapper<WebPost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_del", 1);
        updateWrapper.set("delete_by", ShiroTools.getId());
        updateWrapper.set("delete_name", ShiroTools.getName());
        updateWrapper.set("delete_time", LocalDateTime.now());
        updateWrapper.in("id", ids);


        int state = mapper.update(updateWrapper);
        if (state < 1) {
            return false;
        }
        //重新排序
        List<WebPost> webPosts = listByIds(ids);
        for (WebPost webPost : webPosts) {
            sortUtil.del(webPost, webPost.getClass(), "web_post",
                BaseSort.eq().val("is_del", 0)
            );
        }
        return true;
    }

    //查询岗位
    @DS("slave")
    public List<WebPost> list(@Param("ew") QueryWrapper<WebPost> query) {
        return mapper.select(query);
    }

    //通过ids查询岗位集合
    @DS("slave")
    public List<WebPost> listByIds(List<Long> ids) {
        QueryWrapper<WebPost> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询所有非删除状态岗位
    @DS("slave")
    public List<WebPost> listAll() {
        QueryWrapper<WebPost> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询岗位分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize,  @Param("ew") QueryWrapper<WebPost> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebPost> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebPost> list = mapper.select(page, query);
        return DataPage.init().set(count,list);
    }

    //通过id查询岗位
    @DS("slave")
    public WebPost getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询岗位
    @DS("slave")
    public WebPost getByOne(@Param("ew") QueryWrapper<WebPost> query) {
        List<WebPost> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询岗位数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebPost> query) {
        return mapper.selectCount(query);
    }
}
